
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<jsp:include page="../Common/header.jsp"></jsp:include>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
	<section class="content-header">
		<h1>权限设置</h1>
		<ol class="breadcrumb">
			<li><a href="<c:url value="/Admin/index" />"><i
					class="fa fa-dashboard"></i> Home</a></li>

			<c:forEach items="${routers}" var="router">
				<li><i class="${router.icon}"></i><a href="javascript:void(0)">&nbsp&nbsp${router.name
						}</a></li>
			</c:forEach>

		</ol>
	</section>


	<section class="content">
		<div class="page-header"></div>
		<div class="operate panel panel-default">
			<div class="panel-body">
				<div class="pull-right">
					<a onclick="save()" class="btn btn-sm btn-primary">保存</a>
				</div>
			</div>
		</div>
		<!-- 功能操作区域结束 -->
		<!-- 列表显示区域  -->
		<div class="box box-danger">
			<div class="box-header with-border">
				<h3 class="box-title"></h3>

				<div class="box-tools pull-right">
					<button type="button" class="btn btn-box-tool"
						data-widget="collapse">
						<i class="fa fa-minus"></i>
					</button>
					<button type="button" class="btn btn-box-tool" data-widget="remove">
						<i class="fa fa-remove"></i>
					</button>
				</div>
			</div>
			<div class="box-body">
				<div class="row">
					<div class="col-sm-6 sub_left_menu ">
						<table class="table table-bordered" id="rolesTable">
							<thead>
								<tr>
									<th>名称</th>
									<th>状态</th>
									<th>排序</th>
								</tr>
							</thead>
							<tbody>

								<c:forEach items="${roles}" var="role">

									<tr id="${role.id }">
										<td>${role.name }</td>
										<td>${role.status eq true?'启动':'禁用'}</td>
										<td>${role.sequence}</td>
									</tr>
								</c:forEach>
							</tbody>
						</table>

					</div>

					<div class="col-sm-6">
						<ul  id="treeDemo" style="overflow: scroll;height: 720px;"
							class="ztree"></ul>
					</div>

				</div>
			</div>
		</div>
      
      
         <form method="post" id="Role_node_form" action="<c:url value="/Role/setMenu" />" ><div class="right"><div class="menuHiddenValue"></div><input id="rid" name="role_id" type="hidden" /></div></form>
	
	
	</section>

</div>
<!-- /.content-wrapper -->
<jsp:include page="../Common/footer.jsp"></jsp:include>
<script type="text/javascript">
	var setting = {
		callback : {
			onCheck : zTreeOnCheck
		},
		check : {
			enable : true,
			autoCheckTrigger : true,
		},
		data : {
			simpleData : {
				enable : true
			}
		}
	};

	var zNodes = ${menus};
	var code;

	function setCheck() {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		type = {
			"Y" : "ps",
			"N" : "ps"
		};
		zTree.setting.check.chkboxType = type;
		showCode('setting.check.chkboxType =  { "Y" : "ps", "N" : "ps" };');
	}
	function showCode(str) {
		if (!code)
			code = $("#code");
		code.empty();
		code.append("<li>" + str + "</li>");
	}

	$.fn.zTree.init($("#treeDemo"), setting, zNodes);
	setCheck();
	$("#py").bind("change", setCheck);
	$("#sy").bind("change", setCheck);
	$("#pn").bind("change", setCheck);
	$("#sn").bind("change", setCheck);
	$.fn.zTree.getZTreeObj("treeDemo").expandAll(true);

	function zTreeOnCheck(event, treeId, treeNode) {
		if (treeNode.checked == true
				&& $(".right .menuHiddenValue input[value=" + treeNode.id + "]").length == 0)
			$(".right .menuHiddenValue").append("<input type='hidden' name='menu_list' value="+treeNode.id+" />");
		else if (treeNode.checked == false)
			$(".right .menuHiddenValue input[value=" + treeNode.id + "]").remove();

	};
	
	 $.ajaxSetup({   
         contentType:"application/x-www-form-urlencoded;charset=utf-8",   
         complete:function(XMLHttpRequest,textStatus){ 
           var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus"); //通过XMLHttpRequest取得响应头，sessionstatus，  
               if(sessionstatus=="timeout"){ 
                   alert("登录超时,请重新登录！");
               //如果超时就处理 ，指定要跳转的页面  
                 window.location.reload();   
               }   
            }   
       }); 
	
	//点击表格行触发AJAX
	$("#rolesTable tbody tr").click(function() {
		$("#rolesTable tbody tr").attr("class", "");
		$(this).attr("class", "active");
        $("#rid").val($(this).attr("id"));
        $.fn.zTree.getZTreeObj("treeDemo").checkAllNodes(false);
        $(".right .menuHiddenValue input").remove();
    	$.post("<c:url value='/Role/getMenusByRole' />", {
			id : $(this).attr("id"),
		}, function(data, status) {
			
			 
			
			if (data == "" || data == null)
				{return;}
			data = eval(data);
			
			$.each(data, function(p1, p2){

				$.fn.zTree.getZTreeObj("treeDemo").checkNode($.fn.zTree.getZTreeObj("treeDemo").getNodeByParam("id",p2.id), true, true,true);

			});
			
		});

	});
	
	function save(){
		$("#Role_node_form").submit();
	}
</script>
